import {useStore} from '@/store'
const store = useStore()

export default function checkPermission(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles
    const permissionRoles = value
    const hasPermission = roles.some(role => {
      return permissionRoles.includes(role)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like v-permission="['admin','editor']"`)
    return false
  }
}
// 必须包含列出的所有权限，元素才显示
export function hasPermission(value){
  const permissions = store.getters.btnResourceList || []
  let flag = true
  for (const v of value) {
    if (!permissions.includes(v)) {
      flag = false
    }
  }
  return flag
}
// 只要包含列出的任意一个权限，元素就会显示
export function hasAnyPermission (value){
  const permissions = store.getters.btnResourceList || []
  let flag = false
  for (const v of value) {
    if (permissions.includes(v)) {
      flag = true
    }
  }
  return flag
}

export function hasPagePermission(value){
  // value: route.name
  const permissions = store.getters.pageResourceList || [];
  let searchValue = (page) => {
    if(page.name===value){return true;}
    if(page.children){
      for(let prop of page.children){
        let res = searchValue(prop);
        if(res){return true;}
      }
    }
    return false;
  }
  let pass = false;
  for(let prop of permissions||[]){
    pass = searchValue(prop);
    if(pass){break;}
  }
  return pass;
}